package com.octopus.kernal.ui.builder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import com.octopus.kernal.controller.AbstractSearchCommand;
import com.octopus.kernal.controller.SearchOperator;
import com.octopus.kernal.dao.BaseDao;
import com.octopus.kernal.dao.GenericHibernateDao;
import com.octopus.kernal.model.BaseStruct;
import com.octopus.kernal.service.BusinessModelService;
import com.octopus.kernal.service.ConfigService;
import com.octopus.kernal.ui.element.BaseElement;
import com.octopus.kernal.ui.element.CheckboxElement;
import com.octopus.kernal.ui.element.DateElement;
import com.octopus.kernal.ui.element.InputElement;
import com.octopus.kernal.ui.element.SelectElement;
import com.octopus.kernal.ui.element.TimeElement;
import com.octopus.kernal.util.xml.Column;
import com.octopus.kernal.util.xml.ElementSearch;
import com.octopus.kernal.util.xml.Entity;
import com.octopus.kernal.util.xml.Field;

@Service
public class SearchTableBuilder<T extends BaseStruct> {
	
	private String sp = System.getProperty("line.separator");
	
	@Autowired
	private BusinessModelService businessModelService;
	
	@Autowired
	private BaseDao genericDao;
	
	public String build(HttpServletRequest request, AbstractSearchCommand<T> search, MessageSource messageSource){
		return this.build(request, search, messageSource, new ArrayList<String>());
	}
	
	public String build(HttpServletRequest request, AbstractSearchCommand<T> search, MessageSource messageSource, List<String> readonlyFields){
		String entityName = search.getEntity().getClass().getSimpleName();
		entityName = entityName.substring(0, entityName.indexOf("Struct"));
		Entity entity = this.businessModelService.getEntityByName(entityName);
		if(entity == null)
			return "";
		StringBuffer html = new StringBuffer();
		int index = 0;
		boolean hasSearch = false;
		for(Field field : entity.getFields()){
			ElementSearch element = field.getElementSearch();
			if(element == null)
				continue;
			hasSearch = true;
			if(index % entity.getLayoutSearch() == 0)
				html.append("<tr>" + sp);
			if(StringUtils.isBlank(element.getId()))
				element.setId(field.getName());
			if(StringUtils.isBlank(element.getName()))
				element.setName(field.getName());
			// Label
			html.append("<td");
			if(element.getLayoutLabel() > 1 && (index%entity.getLayoutSearch() + element.getLayoutLabel() + element.getLayoutValue()) <= entity.getLayoutSearch()){
				html.append(" colspan=\"" + element.getLayoutLabel() + "\"");
				index += element.getLayoutLabel();
			}else{
				index ++;
			}
			html.append(">" + sp);
			String key = "label." + entity.getAlias() + ".field." + field.getName();
			html.append(messageSource.getMessage(key, new Object[] { }, LocaleContextHolder.getLocale()));
			html.append(":" + sp);
			html.append("</td>" + sp);
			// Value
			html.append("<td");
			if(element.getLayoutValue() > 1 && (index%entity.getLayoutSearch() + element.getLayoutValue()) <= entity.getLayoutSearch()){
				html.append(" colspan=\"" + element.getLayoutValue() + "\"");
				index += element.getLayoutValue();
			}else{
				index ++;
			}
			html.append(">" + sp);
			try {
				html.append(this.getHtmlElementString(request, field, search, readonlyFields.contains(field.getName()), messageSource));
			} catch (Exception e) {
				e.printStackTrace();
			}
			html.append("</td>" + sp);
			if(index % entity.getLayoutSearch() == 0)
				html.append("</tr>" + sp);
		}
		if(html.lastIndexOf("</tr>") != html.length() - 5){
			html.append("</tr>");
		}
		html.append("<tr><td>");
		String kw = messageSource.getMessage("label.common.keyword", new Object[] { }, LocaleContextHolder.getLocale()) + ":";
		String tips = messageSource.getMessage("label." + entity.getAlias() + ".keyword.tips", new Object[] { }, LocaleContextHolder.getLocale());
		String sb = messageSource.getMessage("label.common.search", new Object[] { }, LocaleContextHolder.getLocale());
		if(entity.getKeywords() != null && entity.getKeywords().size() > 0){
			hasSearch = true;
			InputElement inputElement = new InputElement();
			inputElement.setName("keyword");
			Object valueo = null==search.getOperator()?null:search.getOperator().getKeyword();
			String colspan = entity.getLayoutSearch() == 2?"":"colspan=\"" + (entity.getLayoutSearch() - 1) + "\"";
			html.append(kw + "</td><td " + colspan + ">" + getStringConnect(inputElement, valueo, false, messageSource, true) 
					+ "<input type=\"text\" id=\"entity.keyword\" name=\"entity.keyword\"" 
					+ (null==search.getEntity().getKeyword()?"":" value=\"" 
					+ search.getEntity().getKeyword() + "\" ") + ">");
			html.append("<font size=\"1\" color=\"gray\">" + tips + "</font>");
		}
		if(hasSearch)
			html.append("<input type=\"submit\" value=\"" + sb + "\">");
		html.append("</td></tr>");
		return html.toString();
	}
	
	@SuppressWarnings("rawtypes")
	private String getHtmlElementString(HttpServletRequest request, Field field, AbstractSearchCommand<T> search, boolean readonly, MessageSource messageSource) throws Exception{
		ElementSearch element = field.getElementSearch();
		Column column = field.getColumn();
		Object value = search.getEntity().getAttributeValues().get(field.getName());
		Object value2 = null==search.getEntity2()?null:search.getEntity2().getAttributeValues().get(field.getName());
		Object valueo = null==search.getOperator()?null:search.getOperator().getAttributeValues().get(field.getName());
		if(element == null)
			return "";
		BaseElement htmlElement = null;
		BaseElement htmlElement2 = null;
		String operator = null;
		// Input text
		if(element.getInputElement() != null){
			InputElement inputElement = element.getInputElement();
			htmlElement = element.getInputElement();
			if(inputElement.getDatatype().equals("String")){
				operator = getStringConnect(inputElement, valueo, readonly, messageSource, false);
			}else if(inputElement.getDatatype().equals("Number")){
				operator = getNumberConnect(inputElement, valueo, readonly, messageSource);
				htmlElement2 = element.getInputElement();
			}
			htmlElement.setValue(value);
		// Input date 
		}else if(element.getDateElement() != null){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", new Locale("en"));
			DateElement dateElement = element.getDateElement();
			htmlElement = dateElement;
			htmlElement2 = element.getDateElement();
			valueo = null==search.getOperator()?null:search.getOperator().getAttributeValues().get(field.getName() + "Str");
			operator = getDateConnect(dateElement, valueo, readonly, messageSource);
			htmlElement.setValue(null==value?null:format.format(value));
			value2 = null==value2?null:format.format(value2);
		// Input time
		}else if(element.getTimeElement() != null){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", new Locale("en"));
			TimeElement timeElement = element.getTimeElement();
			htmlElement = timeElement;
			htmlElement2 = element.getTimeElement();
			valueo = null==search.getOperator()?null:search.getOperator().getAttributeValues().get(field.getName() + "Str");
			operator = getTimeConnect(timeElement, valueo, readonly, messageSource);
			htmlElement.setValue(null==value?null:format.format(value));
			value2 = null==value2?null:format.format(value2);
		// Checkbox
		}else if(element.getCheckboxElement() != null){
			CheckboxElement checkboxElement = element.getCheckboxElement();
			checkboxElement.setMessageSource(messageSource);
			htmlElement = checkboxElement;
			operator = getBooleanConnect(checkboxElement, valueo , readonly, messageSource);
			htmlElement.setValue(value);
		// Single select
		}else if(element.getSelectElement() != null){
			SelectElement selectElement = element.getSelectElement();
			selectElement.setMessageSource(messageSource);
			htmlElement = selectElement;
			if("String".equals(column.getType())){
				String[] scopes = null;
				if(selectElement.getLabelProperty() != null)
					scopes = ConfigService.getInstance().getProperties("common.single.select." + selectElement.getLabelProperty());
				if(scopes != null && scopes.length > 0){
					selectElement.setEntitiesScrope(new ArrayList<Object>());
					for(String entity : scopes)
						selectElement.getEntitiesScrope().add(entity);
				}
				operator = getSelectConnect(selectElement, valueo, readonly, messageSource);
			}else if("Integer".equals(column.getType()) || "Long".equals(column.getType())){
				String[] scopes = null;
				if(selectElement.getLabelProperty() != null)
					scopes = ConfigService.getInstance().getProperties("common.single.select." + selectElement.getLabelProperty());
				if(scopes != null && scopes.length > 0){
					selectElement.setEntitiesScrope(new ArrayList<Object>());
					for(String entity : scopes)
						selectElement.getEntitiesScrope().add(Integer.valueOf(entity));
				}
				operator = getSelectConnect(selectElement, valueo, readonly, messageSource);
			}else if("Boolean".equals(column.getType())){
				
			}else if("Date".equals(column.getType()) || "Time".equals(column.getType())){
				
			}else{
				List<Object> list2 = new ArrayList<Object>();
				if(StringUtils.isBlank(selectElement.getScope())){
					Class<BaseStruct> refer = this.businessModelService.getReferenceStruct(search.getEntity(), field.getName());
					String coltype = field.getColumn().getType();
					String seatype = field.getElementSearch().getSelectElement().getType();
					if(!coltype.equals(seatype)){
						String refcol = seatype.substring(seatype.indexOf("(")+1, seatype.indexOf(")"));
						refer = this.businessModelService.getReferenceStruct(refer.newInstance(), refcol);
					}
					GenericHibernateDao<BaseStruct, Long> dao = new GenericHibernateDao<BaseStruct, Long>(refer);
					dao.setSessionFactory(genericDao.getSessionFactory());
					Entity entity = businessModelService.getEntityByClass(refer);
					String[] propertyNames = null;
					if(entity.getDefaultSort() != null){
						propertyNames = new String[]{entity.getDefaultSort()};
					}
					List<BaseStruct> list = dao.findEqualByEntity(null, propertyNames);
					if(list != null){
						for(BaseStruct s : list)
							list2.add(s);
					}
				}else{
					list2 = this.businessModelService.getScope(selectElement.getScope());
				}
				selectElement.setEntitiesScrope(list2);
				valueo = null==search.getOperator()?null:search.getOperator().getAttributeValues().get(field.getName() + "Id");
				operator = getSelectConnect(selectElement, valueo, readonly, messageSource);
			}
			if(value != null && value instanceof Set){
				htmlElement.setValue(((Set)value).toArray()[0]);
			}else{
				htmlElement.setValue(value);
			}
		}
		if(readonly){
			if(htmlElement != null)
				htmlElement.setDisabled(true);
			if(htmlElement2 != null)
				htmlElement2.setDisabled(true);
		}else{
			//this.setElementDisable(htmlElement, htmlElement2, valueo);
		}
		if(htmlElement != null){
			htmlElement.setRequest(request);
			String name = htmlElement.getName();
			htmlElement.setId("entity." + name);
			htmlElement.setName("entity." + name);
			String result = operator + htmlElement.toString();
			htmlElement.setName(name);
			htmlElement.setId(name);
			if(htmlElement2 != null){
				htmlElement2.setId("entity2." + name);
				htmlElement2.setName("entity2." + name);
				htmlElement2.setValue(value2);
				result += "&nbsp;" + htmlElement2.toString();
				htmlElement2.setName(name);
				htmlElement2.setId(name);
				htmlElement2.setValue(value);
			}
			return result;
		}
		return "";
	}
	
	private String getStringConnect(InputElement element, Object value, boolean disabled, MessageSource messageSource, boolean like){
		StringBuffer str = new StringBuffer();
		if(element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<select onchange=\"onOperatorChange(this,'" + element.getName() + "');\" style=\"width:50px;display:none;\"");
			str.append("id=\"operator." + element.getName() + "\" ");
			str.append("name=\"operator." + element.getName() + "\" ");
		}else{
			str.append("<select style=\"width:50px;\"");
		}
		if(disabled)
			str.append("disabled=\"disabled\" ");
		str.append(">");
		//str.append("<option></option>");
		if(!like){
			str.append("<option value=\"" + SearchOperator.EQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.EQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">=</option>");
			str.append("<option value=\"" + SearchOperator.LIKE.ordinal() + "\" " + (String.valueOf(SearchOperator.LIKE.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">~</option>");
		}else{
			str.append("<option value=\"" + SearchOperator.LIKE.ordinal() + "\" " + (String.valueOf(SearchOperator.LIKE.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">~</option>");
			str.append("<option value=\"" + SearchOperator.EQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.EQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">=</option>");
		}
		str.append("</select>");
		if(disabled && element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<input type=\"hidden\" name=\"operator." + element.getName() + "\" value=\"" + SearchOperator.EQUAL.ordinal() + "\">");
		}
		str.append("&nbsp;");
		return str.toString();
	}
	
	private String getNumberConnect(InputElement element, Object value, boolean disabled, MessageSource messageSource){
		StringBuffer str = new StringBuffer();
		if(element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<select onchange=\"onOperatorChange(this,'" + element.getName() + "');\" style=\"width:50px;display:none;\"");
			str.append("id=\"operator." + element.getName() + "\" ");
			str.append("name=\"operator." + element.getName() + "\" ");
		}else{
			str.append("<select style=\"width:50px;\"");
		}
		if(disabled)
			str.append("disabled=\"disabled\" ");
		str.append(">");
		//str.append("<option></option>");
		str.append("<option value=\"" + SearchOperator.EQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.EQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">=</option>");
		str.append("<option value=\"" + SearchOperator.NOTEQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.NOTEQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">!=</option>");
		str.append("<option value=\"" + SearchOperator.GREATER.ordinal() + "\" " + (String.valueOf(SearchOperator.GREATER.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">>=</option>");
		str.append("<option value=\"" + SearchOperator.LESS.ordinal() + "\" " + (String.valueOf(SearchOperator.LESS.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + "><=</option>");
		str.append("<option value=\"" + SearchOperator.BETWEEN.ordinal() + "\" " + (String.valueOf(SearchOperator.BETWEEN.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">[ , ]</option>");
		str.append("</select>");
		if(disabled && element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<input type=\"hidden\" name=\"operator." + element.getName() + "\" value=\"" + SearchOperator.EQUAL.ordinal() + "\">");
		}
		str.append("&nbsp;");
		return str.toString();
	}
	
	private String getDateConnect(DateElement element, Object value, boolean disabled, MessageSource messageSource){
		StringBuffer str = new StringBuffer();
		if(element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<select onchange=\"onOperatorChange(this,'" + element.getName() + "');\" style=\"width:50px;display:none;\"");
			str.append("id=\"operator." + element.getName() + "\" ");
			str.append("name=\"operator." + element.getName() + "\" ");
		}else{
			str.append("<select style=\"width:50px;\"");
		}
		if(disabled)
			str.append("disabled=\"disabled\" ");
		str.append(">");
		//str.append("<option></option>");
		str.append("<option value=\"" + SearchOperator.BETWEEN.ordinal() + "\" " + (String.valueOf(SearchOperator.BETWEEN.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">[ , ]</option>");
		str.append("<option value=\"" + SearchOperator.EQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.EQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">=</option>");
		str.append("<option value=\"" + SearchOperator.NOTEQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.NOTEQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">!=</option>");
		str.append("<option value=\"" + SearchOperator.GREATER.ordinal() + "\" " + (String.valueOf(SearchOperator.GREATER.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">>=</option>");
		str.append("<option value=\"" + SearchOperator.LESS.ordinal() + "\" " + (String.valueOf(SearchOperator.LESS.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + "><=</option>");
		str.append("</select>");
		if(disabled && element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<input type=\"hidden\" name=\"operator." + element.getName() + "\" value=\"" + SearchOperator.EQUAL.ordinal() + "\">");
		}
		str.append("&nbsp;");
		return str.toString();
	}
	
	private String getTimeConnect(TimeElement element, Object value, boolean disabled, MessageSource messageSource){
		StringBuffer str = new StringBuffer();
		if(element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<select onchange=\"onOperatorChange(this,'" + element.getName() + "');\" style=\"width:50px;display:none;\"");
			str.append("id=\"operator." + element.getName() + "\" ");
			str.append("name=\"operator." + element.getName() + "\" ");
		}else{
			str.append("<select style=\"width:50px;\"");
		}
		if(disabled)
			str.append("disabled=\"disabled\" ");
		str.append(">");
		//str.append("<option></option>");
		str.append("<option value=\"" + SearchOperator.BETWEEN.ordinal() + "\" " + (String.valueOf(SearchOperator.BETWEEN.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">[ , ]</option>");
		str.append("<option value=\"" + SearchOperator.EQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.EQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">=</option>");
		str.append("<option value=\"" + SearchOperator.NOTEQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.NOTEQUAL.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">!=</option>");
		str.append("<option value=\"" + SearchOperator.GREATER.ordinal() + "\" " + (String.valueOf(SearchOperator.GREATER.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + ">>=</option>");
		str.append("<option value=\"" + SearchOperator.LESS.ordinal() + "\" " + (String.valueOf(SearchOperator.LESS.ordinal()).equals(String.valueOf(value))?"selected=\"selected\"":"") + "><=</option>");
		str.append("</select>");
		if(disabled && element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<input type=\"hidden\" name=\"operator." + element.getName() + "\" value=\"" + SearchOperator.EQUAL.ordinal() + "\">");
		}
		str.append("&nbsp;");
		return str.toString();
	}
	
	private String getBooleanConnect(CheckboxElement element, Object value, boolean disabled, MessageSource messageSource){
		StringBuffer str = new StringBuffer();
		if(element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<select onchange=\"onOperatorChange(this,'" + element.getName() + "');\" style=\"width:50px;display:none;\"");
			str.append("id=\"operator." + element.getName() + "\" ");
			str.append("name=\"operator." + element.getName() + "\" ");
		}else{
			str.append("<select style=\"width:50px;\"");
		}
		if(disabled)
			str.append("disabled=\"disabled\" ");
		str.append(">");
		//str.append("<option></option>");
		str.append("<option value=\"" + SearchOperator.EQUAL.ordinal() + "\" " + (value!=null?"selected=\"selected\"":"") + ">=</option>");
		str.append("</select>");
		if(disabled && element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<input type=\"hidden\" name=\"operator." + element.getName() + "\" value=\"" + SearchOperator.EQUAL.ordinal() + "\">");
		}
		str.append("&nbsp;");
		return str.toString();
	}
	
	@SuppressWarnings("rawtypes")
	private String getSelectConnect(SelectElement element, Object value, boolean disabled, MessageSource messageSource){
		String operator = "-1";
		if(value == null){
			
		}else if(value instanceof String){
			operator = (String)value;
		}else if(value instanceof Long || value instanceof Integer){
			operator = String.valueOf(value);
		}else if(value instanceof Set){
			operator = String.valueOf(((Set) value).toArray()[0]);
		}
		StringBuffer str = new StringBuffer();
		if(element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<select onchange=\"onOperatorChange(this,'" + element.getName() + "');\" style=\"width:50px;display:none;\"");
			str.append("id=\"operator." + element.getName() + "\" ");
			str.append("name=\"operator." + element.getName() + "\" ");
		}else{
			str.append("<select style=\"width:50px;\"");
		}
		if(disabled)
			str.append("disabled=\"disabled\" ");
		str.append(">");
		//str.append("<option></option>");
		str.append("<option value=\"" + SearchOperator.EQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.EQUAL.ordinal()).equals(operator)?"selected=\"selected\"":"") + ">=</option>");
		str.append("<option value=\"" + SearchOperator.NOTEQUAL.ordinal() + "\" " + (String.valueOf(SearchOperator.NOTEQUAL.ordinal()).equals(operator)?"selected=\"selected\"":"") + ">!=</option>");
		str.append("</select>");
		if(disabled && element != null && StringUtils.isNotBlank(element.getName())){
			str.append("<input type=\"hidden\" name=\"operator." + element.getName() + "\" value=\"" + SearchOperator.EQUAL.ordinal() + "\">");
		}
		str.append("&nbsp;");
		return str.toString();
	}
	
	@SuppressWarnings({ "rawtypes", "unused" })
	private void setElementDisable(BaseElement element, BaseElement element2, Object value){
		String operator = "-1";
		if(value == null){
			
		}else if(value instanceof String){
			operator = (String)value;
		}else if(value instanceof Long || value instanceof Integer){
			operator = String.valueOf(value);
		}else if(value instanceof Boolean){
			if((Boolean)value)
				operator = String.valueOf(SearchOperator.EQUAL.ordinal());
		}else if(value instanceof Set){
			operator = String.valueOf(((Set) value).toArray()[0]);
		}
		if(String.valueOf(SearchOperator.EQUAL.ordinal()).equals(operator)){
			if(element != null)
				element.setDisabled(false);
			if(element2 != null)
				element2.setDisabled(true);
		}else if(String.valueOf(SearchOperator.NOTEQUAL.ordinal()).equals(operator)){
			if(element != null)
				element.setDisabled(false);
			if(element2 != null)
				element2.setDisabled(true);
		}else if(String.valueOf(SearchOperator.LIKE.ordinal()).equals(operator)){
			if(element != null)
				element.setDisabled(false);
			if(element2 != null)
				element2.setDisabled(true);
		}else if(String.valueOf(SearchOperator.GREATER.ordinal()).equals(operator)){
			if(element != null)
				element.setDisabled(false);
			if(element2 != null)
				element2.setDisabled(true);
		}else if(String.valueOf(SearchOperator.LESS.ordinal()).equals(operator)){
			if(element != null)
				element.setDisabled(false);
			if(element2 != null)
				element2.setDisabled(true);
		}else if(String.valueOf(SearchOperator.BETWEEN.ordinal()).equals(operator)){
			if(element != null)
				element.setDisabled(false);
			if(element2 != null)
				element2.setDisabled(false);
		}else{
			if(element != null)
				element.setDisabled(true);
			if(element2 != null)
				element2.setDisabled(true);
		}
	}

}